package com.example.listener;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener;
import org.springframework.stereotype.Component;

@Component
public class OrderListener implements ChannelAwareMessageListener {
//    延迟队列 监听的是死信队列
    @RabbitListener(queues = "order_queue_dlx")
    @Override
    public void onMessage(Message message, Channel channel) throws Exception {
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        try {
            //1. 接受转换消息
            System.out.println(new String(message.getBody()));
//        2、处理业务逻辑
            System.out.println("处理业务逻辑");
            System.out.println("根据订单id查询其状态");
            System.out.println("判断状态是否是支付成功");
            System.out.println("取消订单，回滚库存");
//        3. 手动签收
            channel.basicAck(deliveryTag,true);
        } catch (Exception e) {
//            4. 拒绝签收,不重回队列 requeue=false
            System.out.println("拒绝接受");
//            第三个参数：requeue：重回队列。如果设置为true,则消息重新回到queue 然后broker会重新发送该消息
            channel.basicNack(deliveryTag, true, false);
        }
    }
}
